<?php
namespace Crm\Api;

use PhalApi\Api;
use PhalApi\Request;
use PhalApi\Exception\BadRequestException;
use Auth\Api\Token as Token;
use Crm\Domain\Esf as DomainEsf;


/**
 * 二手房接口服务
 *
 * @author: luodong <751450467@qq.com> 2020-01-04
 */

class Esf extends Api {

    /**
     * token对象
     */
    public $token;

    /**
     * 当前用户信息
     */
    public $user;

    public function __construct()
    {
        //允许所有域名发起的跨域请求
        header('Access-Control-Allow-Origin: *');  //支持全域名访问，不安全，部署后需要固定限制为客户端网址
        //header('Access-Control-Allow-Methods:POST,GET,OPTIONS,DELETE'); //支持的http 动作
        //header('Access-Control-Allow-Headers:Authorization,content-type');  //响应头 请按照自己需求添加。
        //$this->di = \PhalApi\DI();
        $this->token = new Token();

    }

    public function getRules() {
        return array(
            //房源列表
            'getList' => array(),
            'add'=>array(
                'project_id' => array('name' => 'project_id', 'type'=>'int', 'require' => true, 'desc' => '楼盘ID')
            ),
            'update'=>array(
                'esf_id' => array('name' => 'esf_id', 'type'=>'int', 'require' => true, 'desc' => '房源ID')
            ),
            'pics'=>array(
                'esf_id' => array('name' => 'esf_id', 'type'=>'int', 'require' => true, 'desc' => '房源ID')
            ),
            'showTel'=>array(
                'esf_id' => array('name' => 'esf_id', 'type'=>'int', 'require' => true, 'desc' => '房源ID')
            ),
            'showHouseNo'=>array(
                'esf_id' => array('name' => 'esf_id', 'type'=>'int', 'require' => true, 'desc' => '房源ID')
            ),
            'isFollow'=>array(),
            'lastFollow'=>array(),
            'checkHouseExist'=>array(
                'project_id' => array('name' => 'project_id', 'type'=>'int', 'require' => true, 'desc' => '楼盘ID'),
                'build_no' => array('name' => 'build_no', 'type'=>'string', 'require' => true, 'desc' => '楼栋号'),
                'unit_no' => array('name' => 'unit_no', 'type'=>'string', 'require' => true, 'desc' => '单元号'),
                'house_no' => array('name' => 'house_no', 'type'=>'string', 'require' => true, 'desc' => '房号'),
                'house_type' => array('name' => 'house_type', 'type'=>'int', 'require' => true, 'desc' => '房源类型1出租2出售')
            ),
            'detail' => array(
                'esf_id' => array('name' => 'esf_id', 'type'=>'int', 'require' => true, 'desc' => '房源ID')
            ),
            'detailShare' => array(
                'esf_id' => array('name' => 'esf_id', 'type'=>'int', 'require' => true, 'desc' => '房源ID')
            ),
            'baseInfo' => array(
                'esf_id' => array('name' => 'esf_id', 'type'=>'int', 'require' => true, 'desc' => '房源ID')
            ),
            'detailBis' => array(
                'esf_id' => array('name' => 'esf_id', 'type'=>'int', 'require' => true, 'desc' => '房源ID')
            ),
        );

    }

    /**
     * 返回房源列表
     * @desc 根据搜索条件返回房源列表(如项目名称、楼栋、单元、价格、面积等)
     * @return obj 房源列表
     */
    public function getList() {
        $this->user = $this->token->checkToken();

        $domain = new DomainEsf();
        $rs = $domain->search($this->user);
        return $rs;
    }

    /**
     * 添加房源
     * @desc 添加房源
     * @return obj 添加房源
     */
    public function add() {
        $this->user = $this->token->checkToken();
        $params =  \PhalApi\DI()->request->getAll();

        $domain = new DomainEsf();
        $rs = $domain->add($params,$this->user);
        return $rs;
    }

    /**
     * 更新房源
     * @desc 更新房源
     * @return obj 更新房源
     */
    public function update() {
        $this->user = $this->token->checkToken();
        $params =  \PhalApi\DI()->request->getAll();

        $domain = new DomainEsf();
        $rs = $domain->update($params,$this->user);
        return $rs;
    }

    /**
     * 房源图片
     * @desc 房源图片
     * @return obj 房源图片
     */
    public function pics() {
        $this->user = $this->token->checkToken();

        $domain = new DomainEsf();
        $rs = $domain->getEsfPic($this->esf_id,0,100,array('type'=>'img'));
        return $rs;
    }

    /**
     * 查看房源电话
     * @desc 查看房源电话
     * @return obj 记录用户查看了业主电话
     */
    public function showTel(){
        $this->user = $this->token->checkToken();

        $domain = new DomainEsf();
        $rs = $domain->showTel($this->esf_id,$this->user);
        return $rs;
    }

    /**
     * 查看房源房号
     * @desc 查看房源房号
     * @return obj 记录用户查看房源房号
     */
    public function showHouseNo(){
        $this->user = $this->token->checkToken();

        $domain = new DomainEsf();
        $rs = $domain->showHouseNo($this->esf_id,$this->user);
        return $rs;
    }

    /**
     * 是否有未跟进房源
     * @desc 是否有未跟进房源
     * @return obj 是否有未跟进房源
     */
    public function isFollow(){
        $this->user = $this->token->checkToken();

        $domain = new DomainEsf();
        $rs = $domain->isNeedEsfFollow($this->user);
        return $rs;
    }

    /**
     * 最近需要填写跟进房源
     * @desc 最近需要填写跟进房源
     * @return obj 获取最近需要填写跟进房源记录
     */
    public function lastFollow(){
        $this->user = $this->token->checkToken();

        $domain = new DomainEsf();
        $rs = $domain->getNeedEsfFollow($this->user);
        return $rs;
    }

    /**
     * 验证房源是否存在
     * @desc 验证房源是否存在
     * @return obj 验证房源信息
     */
    public function checkHouseExist(){
        $this->user = $this->token->checkToken();

        $domain = new DomainEsf();
        $rs = $domain->checkHouseExist($this->project_id,$this->build_no,$this->unit_no,$this->house_no,$this->house_type);
        return $rs;
    }

    /**
     * 返回房源基础信息
     * @desc 根据房源ID返回房源详细信息
     * @return obj 房源详情
     */
    public function baseInfo(){
        $this->user = $this->token->checkToken();
        error_log("esfDetail!", 0);

        $domain = new DomainEsf();
        $data = $domain->getEsfBaseInfo($this->esf_id);

        return $data;
    }

    /**
     * 返回房源详细信息
     * @desc 根据房源ID返回房源详细信息
     * @return obj 房源详情
     */
    public function detail(){
        $this->user = $this->token->checkToken();
        error_log("esfDetail!", 0);

        $domain = new DomainEsf();
        $data = $domain->getEsfInfo($this->esf_id);

        return $data;
    }

    /**
     * 返回房源详细信息
     * @desc 根据房源ID返回房源详细信息
     * @return obj 房源详情
     */
    public function detailShare(){
        $domain = new DomainEsf();
        $data = $domain->getEsfInfo($this->esf_id);

        return $data;
    }

    /**
     * 返回房源业务信息
     * @desc 根据房源ID返回房源业务信息
     * @return obj 房源业务信息
     */
    public function detailBis(){
        $this->user = $this->token->checkToken();
        error_log("esfDetail!", 0);
        //主表数据
        $params =  \PhalApi\DI()->request->getAll();
        //插入主表数据
        $domain = new DomainEsf();
        switch ($params['bisType']){
            case '1'://返回跟进信息
                $data = $domain->getEsfFollow($params,$this->user);
                break;
            case '2'://返回带看信息
                $data = $domain->getEsfVisit($params,$this->user);
                break;
            case '3'://返回议价信息
                $data = $domain->getEsfPrice($params);
                break;
            case '4'://返回抢盘信息
                $data = $domain->getEsfTask($params);
                break;
        }

        return $data;
    }

}
